package com.grantha.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.grantha.util.DB;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class LogoutServlet
 */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log = Logger.getLogger(this.getClass());  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		log.info("Servlet Started");
		HttpSession session =  request.getSession();
		session.setAttribute("CurrentUserName"," ");
		session.setAttribute("CurrentUserRights", " ");
		
		RequestDispatcher rd = request.getRequestDispatcher("jsp/login.jsp");
		rd.include(request, response);
		
		//String username = request.getParameter("username"); 
		//String password = request.getParameter("password");
		
		//log.info("username="+username);
		//log.info("password="+password);
		
	/*	try {
			
			ResultSet rs1= DB.search("select * from users where username=? and password=?",username,password );
			if(rs1.next()){
				username = rs1.getString("username");
				
			}
			/*ResultSet rs2= DB.search("select rights from users where username=? and password=?",username,password );
			if(rs2.next()){
				username = rs2.getString("uesrname");
				
			}*/
			//String rights=rs2;
		/*	
			session.setAttribute("CurrentUserName"," ");
			session.setAttribute("CurrentUserRights", " ");
			
			out.print("success");
			log.info("success");
		} catch (Exception e) {
			out.print("error");
			log.error("Error");
		}*/
		
		//coding ends
		
		out.close();
		log.info("Servlet end");
		// TODO Auto-generated method stub
	}

}
